package adapter.messge_adapter;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;

import activity.personal.ActivityWeiXiuXiangqing;
import base.MyApplcation;
import bean.measge_bean.WeiXiuiJiLuBean;
import util.utls.Const;
import util.utls.TimeFormat;
import util.utls.XImageLoader;


/**
 * 维修记录
 */
public class WeiXiuRecordAdapter extends BaseAdapter {
    Activity context;
    List<WeiXiuiJiLuBean.ListInfo> list;

    public WeiXiuRecordAdapter(Activity context, List<WeiXiuiJiLuBean.ListInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list.size() <= 0) {
            return 0;
        } else {
            return list.size();
        }

    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = LayoutInflater.from(MyApplcation.ctx).inflate(R.layout.item_weixiu, null);
            holder.tvTimeData = (TextView) convertView.findViewById(R.id.tvTimeData);
            holder.weixiu_person = (TextView) convertView.findViewById(R.id.weixiu_person);
            holder.tvDriver = (TextView) convertView.findViewById(R.id.tvDriver);

            holder.tvinformation = (TextView) convertView.findViewById(R.id.tvinformation);
            holder.tvMaintenanceCosts = (TextView) convertView.findViewById(R.id.tvMaintenanceCosts);
            holder.tvMaterialConst = (TextView) convertView.findViewById(R.id.tvMaterialConst);
            holder.weixiu_tv = (TextView) convertView.findViewById(R.id.weixiu_tv);
            holder.tvOk = (TextView) convertView.findViewById(R.id.tvOk);

            holder.img0 = (LinearLayout) convertView.findViewById(R.id.img0);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }
        holder.tvTimeData.setText(TimeFormat.DateFormat(list.get(position).getCreate_time()));//时间
        holder.weixiu_person.setText(list.get(position).getRepair_name());//维修类目
        if (list.get(position).getDriver_realname().equals("")) {
            holder.tvDriver.setText("匿名");//发起司机
        } else {
            holder.tvDriver.setText(list.get(position).getDriver_realname());//发起司机
        }

        holder.tvinformation.setText(list.get(position).getMobile());//联系方式

        String pric = list.get(position).getStuff_price();
        String prics = list.get(position).getRepair_price();
        double d1 = Double.parseDouble(pric);
        double d2 = Double.parseDouble(prics);
        holder.tvMaintenanceCosts.setText("¥:" + d2 / 100);//维修费用
        holder.tvMaterialConst.setText("¥:" + d1 / 100);//材料费用
        holder.weixiu_tv.setText(list.get(position).getOrder_code());//维修单号


        String str = list.get(position).getImages();// 原始字符串

        String[] arrayStr = new String[]{};// 字符数组
        List<String> lists = new ArrayList<String>();// list

        arrayStr = str.split(",");// 字符串转字符数组
        lists = java.util.Arrays.asList(arrayStr);// 字符数组转list
        String[] storeStr = lists.toArray(new String[lists.size()]);// list转成字符数组
        holder.img0.removeAllViews();
        /* 输出字符数组的值 */
        for (String s : arrayStr) {
            System.out.println(s);
            if (storeStr.length <= 0) {

                holder.img0.setVisibility(View.GONE);
            } else if (s == null || s.equals("")) {
                holder.img0.setVisibility(View.GONE);
            } else if (storeStr.length >= 1) {
                holder.img0.setVisibility(View.VISIBLE);
                View v = LayoutInflater.from(MyApplcation.ctx).inflate(R.layout.layout_items, null);
                ImageView img1 = (ImageView) v.findViewById(R.id.img1);
                XImageLoader.load(Const.IMAGE_HEAD + s, img1);
                ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(
                        ViewGroup.MarginLayoutParams.WRAP_CONTENT,
                        ViewGroup.MarginLayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(5, 5, 5, 5);
                holder.img0.addView(v, layoutParams);
            }

        }
        final ArrayList<String> imgs = new ArrayList<String>();
        imgs.add(list.get(position).getImages());
        Log.d("werwerwer", list.get(position).getImages() + "");
        holder.tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, ActivityWeiXiuXiangqing.class);
                intent.putExtra("WeiXiu1", list.get(position).getPrice());
                intent.putExtra("WeiXiu2", list.get(position).getStuff_price());
                intent.putExtra("WeiXiu3", list.get(position).getRepair_price());
                intent.putExtra("WeiXiu4", list.get(position).getDetail());
                intent.putExtra("info", list.get(position).getImages());
                context.startActivity(intent);

            }
        });
        return convertView;
    }

    class viewHolder {
        TextView tvTimeData;
        TextView weixiu_person;
        TextView tvDriver;
        TextView tvinformation;
        ImageView img1, img2, img3;
        TextView tvMaintenanceCosts;
        LinearLayout img0;
        TextView tvMaterialConst, weixiu_tv, tvOk;
    }
}
